package com.xthh._function;



import com.xthh._struct.HH_T_SYSTEMTIME;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;


public class HH_TimeUtil {
    public static String GetCurrentSystemTimeD() {
        SimpleDateFormat sDateFormat;
        sDateFormat = new SimpleDateFormat("yyyyMMdd");
        return sDateFormat.format(new Date());
    }

    public static String GetCurrentSystemTime() {
        SimpleDateFormat sDateFormat;
        sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss-SSS");
        return sDateFormat.format(new Date());
    }

    public static String GetCurrentSystemTimeF() {
        SimpleDateFormat sDateFormat;
        sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return sDateFormat.format(new Date());
    }

    public static String GetCurrentSystemTimeH() {
        SimpleDateFormat sDateFormat;
        sDateFormat = new SimpleDateFormat("yyyyMMddHH");
        return sDateFormat.format(new Date());
    }

    public static String GetCurrentSystemTimeYear() {
        SimpleDateFormat sDateFormat;
        sDateFormat = new SimpleDateFormat("yyyy");
        return sDateFormat.format(new Date());
    }

    public static HH_T_SYSTEMTIME GetLocalTime() {
        HH_T_SYSTEMTIME st = new HH_T_SYSTEMTIME();
        Calendar calendar = Calendar.getInstance();
        st.year = calendar.get(Calendar.YEAR);
        st.month = calendar.get(Calendar.MONTH) + 1;
        st.day = calendar.get(Calendar.DAY_OF_MONTH);
        st.hour = calendar.get(Calendar.HOUR_OF_DAY);
        st.minute = calendar.get(Calendar.MINUTE);
        st.second = calendar.get(Calendar.SECOND);

        return st;
    }

    // 将时间转换为时间戳
    // "yyyy-MM-dd HH:mm:ss"
    public static long dateToStamp(String s) {
        long result = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = simpleDateFormat.parse(s);
            return date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

    // 将时间戳转换为时间(单位是毫秒)
    public static String StampToTime(long lt) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    // 生成时间戳(秒)
    public static long MadeMillisTime() {
        return System.currentTimeMillis() / 1000;
    }

    // 毫秒
    public static long MadeMillisTimeMS() {
        return System.currentTimeMillis();
    }
    // 比较两个时间相差几天
    // int daynumber = dateDiff(startDate, endDate, "yyyyMMdd");
    // startTime是较早的时间
    // endTime是现在的时间
    public static long DateDiffDay(String startTime, String endTime, String format) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + hour + "小时" + min + "分钟" + sec + "秒。");
            if (day >= 1) {
                return day;
            } else {
                if (day == 0) {
                    return 1;
                } else {
                    return 0;
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static long DateDiffHour(String startTime, String endTime, String format) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long hour = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            hour = diff / nh;// 计算差多少小时

            if (hour >= 1) {
                return hour;
            } else {
                if (hour == 0) {
                    return 1;
                } else {
                    return 0;
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    // 时间字符串转时间
    public static Date ParseServerTime(String serverTime, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        Date date = null;
        try {
            date = sdf.parse(serverTime);
        } catch (Exception e) {

        }
        return date;
    }
}
